﻿using System;
using System.Collections.Generic;
using System.Text;
using com.lover.astd.common.logic;
using com.lover.astd.common.model;

namespace com.lover.astd.common.logicexe.temp
{
    public class EquipMeltExe : TempExeBase, ITempExe
    {
        private int _equipId = 0;
        private bool _finished = false;
        private Equipment _eqp = null;
        private int _equipLevel = -1;
        private int _equipMh = -1;
        public EquipMeltExe() { _name = ConfigStrings.S_Temp; _readable = ConfigStrings.SR_Temp; }
        public override long execute()
        {
            EquipMgr mgr = ServiceFactory.getEquipManager();
            if (_equipLevel < 0)
            {

                List<Equipment> equips = mgr.getStoreCanMelt(_proto, _logger, _user);
                foreach (Equipment eq in equips)
                {
                    if (eq.Id == _equipId) { _eqp = eq; break; }
                }
                if (_eqp == null)
                {
                    _finished = true;
                    return immediate();
                }
                else
                {
                    _finished = false;
                    _equipLevel = _eqp.Level;
                    _equipMh = _eqp.EnchantLevel;
                }
            }
            int result = 0;
            if (_equipLevel > 1)
            {
                while (_equipLevel > 1)
                {
                    result = mgr.degradeEquip(_proto, _logger, _equipId);
                    if (result > 0) { _finished = true; return immediate(); }
                    _eqp.Level = _eqp.Level - 1;
                    _equipLevel--;
                }
            }
            else if (_equipMh > 0)
            {
                while (_equipMh > 0)
                {
                    result = mgr.degradeMh(_proto, _logger, _equipId);
                    if (result > 0) { _finished = true; return immediate(); }
                    _eqp.EnchantLevel = _eqp.EnchantLevel - 1;
                    _equipMh--;
                }
            }
            else
            {
                if (_eqp.Quality >= EquipmentQuality.Yellow)
                {
                    int magic = _user.Magic;
                    result = mgr.meltEquip(_proto, _logger, _equipId, magic);
                    if (result > 0) { _finished = true; return immediate(); }
                }
                else
                {
                    result = mgr.sellEquip(_proto, _logger, _equipId);
                    if (result > 0) { _finished = true; return immediate(); }
                }
                _finished = true;
            }
            return immediate();
        }
        #region ITempExe 成员
        public bool isFinished()
        {
            return _finished;
        }
        public void setTarget(Dictionary<string, string> conf)
        {
            if (conf == null || !conf.ContainsKey("equipId")) return;
            Int32.TryParse(conf["equipId"], out _equipId);
        }
        public string getStatus()
        {
            if (!_finished) return "降级/融化进行中"; else return "完成";
        }
        #endregion
    }
}
